package com.fish.admin.module.auth.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.http.Header;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.fish.admin.module.auth.service.AuthInfoService;
import com.fish.admin.module.system.service.SysAuthorityService;
import com.fish.common.core.base.AuthenticationRequest;
import com.fish.common.core.base.AuthenticationResponse;
import com.fish.common.core.base.AuthorizationInfo;
import com.fish.common.core.enums.UserTypeEnum;
import com.fish.common.core.response.RestResponse;
import com.fish.common.web.util.HttpServletUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/auth")
@Slf4j
@RequiredArgsConstructor
public class AuthController {


    private final AuthInfoService authInfoService;

    private final SysAuthorityService sysAuthorityService;

    /**
     * 系统用户登陆
     * @return
     */
    @SaIgnore
    @PostMapping("/v1/system/token")
    public RestResponse<AuthenticationResponse> systemToken(@RequestBody AuthenticationRequest authentication) {
        UserAgent userAgent = UserAgentUtil.parse( HttpServletUtils.getRequest().getHeader(Header.USER_AGENT.toString()));
       return RestResponse.ok(authInfoService.login(authentication, UserTypeEnum.SYSTEM));
    }
    /**
     * 渠道用户登陆
     * @return
     */
    @SaIgnore
    @PostMapping("/v1/tenant/token")
    public RestResponse<AuthenticationResponse> tenantToken(@RequestBody AuthenticationRequest authentication) {
        return RestResponse.ok(authInfoService.login(authentication,UserTypeEnum.TENANT));
    }

    /**
     * 业务员登陆
     * @return
     */
    @SaIgnore
    @PostMapping("/v1/salesman/token")
    public RestResponse<AuthenticationResponse> salesmanToken(@RequestBody AuthenticationRequest authentication) {
        return RestResponse.ok(authInfoService.login(authentication,UserTypeEnum.SALESMAN));
    }



    /**
     * 获取当前登录登录用户的授权信息
     * @return
     */
    @GetMapping("/v1/authorization")
    public RestResponse<AuthorizationInfo> authorization() {
        return RestResponse.ok(sysAuthorityService.authorization());
    }



}
